<template>
  <el-dialog width="80%" :close-on-click-modal="false" :title="title" :model-value="isShow" @open="onOpen">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="70px">
      <el-form-item label="标题" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="编码" prop="id">
        <el-input v-model="form.id" />
      </el-form-item>
    
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="排序" prop="seq">
        <el-input v-model="form.seq" />
      </el-form-item>
      <el-form-item style="margin-bottom: 0" label="所属应用" prop="type">
        <el-select v-model="form.projectId" placeholder="请选择">
          <el-option v-for="item in applications" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-bottom: 0" label="上级类目" prop="parentId">

        <el-tree-select v-model="form.parentId" :data="permissionOption" :props="normalizer" value-key="id" check-strictly />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSave">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDetail, add, save, queryData } from "@/api/admin/resource";
import { getAll as getAllProject } from "@/api/admin/project";

export default {
  components: {},
  props: {
    data: {
      required: true,
      type: String,
      default: "",
    },
    isShow: {
      require: true,
      type: Boolean,
      default: false,
    },
    title: {
      require: true,
      type: String,
      default: "",
    },
  },
  data() {
    return {
      form: {},
      rules: {},
      options: [],
      applications: [],
      normalizer: {

          label: 'name',
          children: 'child',

      },
    
      checkRows: []
    };
  },
  emits: ['onChange', 'update:isShow'],
  mounted() {
    queryData().then((res) => {
      this.options = res.result;
    });

    getAllProject().then((res) => {
      this.applications = res.result
    })
  },
  methods: {
  
    async onOpen() {
      if (this.data) {

        const user = await getDetail(this.data);
        this.form = user.result;

       
      } else {
        this.form = {
          id: "",
          parentId: "",
          name: "",
          description: "",
          level: 1,
          seq: 1,
          projectId: ""
        };
      }
    },
    async onSave() {
      if (this.data) {
      await save(this.form)
      }
      else {
        await add(this.form)
      }
      this.$emit("onChange", this.form);
      this.$emit("update:isShow", false)
    },
    onCancel() {
      this.$emit("update:isShow", false)
    },
    onClose() { },
  },
  watch: {
    data: async function () { },
  },
  computed: {
    permissionOption() {
      return this.options.filter(t => t.projectId == this.form.projectId);
    }
  }
};
</script>


